﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using web_search;

namespace User_subsystem
{
    public partial class FormLogon : Form
    {
        public FormLogon()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
        private bool IsStringobsessnumber(string str)
        {
            for(int i=0; i<str.Length; i++)
            {
                if (str[i] > '0' && str[i] < '9')
                {
                    return false;
                }  
            }
            return true;

        }
        private bool IsStringobsessword(string str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                if ((str[i] > 'a' && str[i] < 'z') || (str[i] > 'A' && str[i] < 'Z'))
                {
                    return false;
                }
            }
            return true;
        }
        private bool IsnoStringobsessnumber(string str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] < '0' || str[i] > '9')
                {
                    return false;
                }
            }
            return true;

        }
        private void button1_Click(object sender, EventArgs e)
        {
            //先判断文本框是否为空
            if (textname.Text == "" || textphone.Text == ""  
                || textpassword.Text == "" || textagainpass.Text == "" )
            {
                //提示有空项
                MessageBox.Show("有空项,请检查后输入。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //判断合法性
            //名字是否含有数字
            if (!(IsStringobsessnumber(textname.Text)))
            {
                //名字含有数字
                MessageBox.Show("名字含有数字", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //电话号码含有非数字
            if (!(IsnoStringobsessnumber(textphone.Text)))
            {
                //电话号码含有非数字
                MessageBox.Show("电话号码含有非数字", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //电话号码不是11位
            if(textphone .Text .Length != 11)
            {
                MessageBox.Show("电话号码不是11位", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //密码不足6位或者超过20位
            if((textpassword .Text.Length > 20) || (textpassword.Text.Length < 6))
            {
                //密码不足6位或者超过20位
                MessageBox.Show("密码格式不正确，密码长度为6~20位", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (textpassword .Text .Trim ()!=textagainpass .Text .Trim())
            {
                //密码不匹配
                MessageBox.Show("密码不匹配", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //注册  获取数据库中账号的最大值  然后加1
            DaoAdmin dao = new DaoAdmin();
            dao.connect();//连接并且打开数据库
            string sql = "select MAX([UserID]) from [UserInfor]";
            SqlDataReader reader = dao.read(sql);
            reader.Read();
            string id = reader[0].ToString();
            int ida = int.Parse(id)+1;
            id = ida.ToString();// 注册账号
            string name = textname.Text;
            string address = textaddress.Text;
            string phone = textphone.Text;
            string password = textpassword.Text;
            string againpass = textagainpass.Text;
            string status = "用户";
            
            //进行添加
            sql = $"insert into [UserInfor] values('{id}','{name}','{phone}','{address}','{password}','{status}')";
            if(dao.Execute(sql) > 0)
            {
                //注册成功
                MessageBox.Show($"注册成功,您的账号是 {id} ", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information) ;
                
                this.Close();
            }
            else
            {
                //注册失败
                MessageBox.Show("注册失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            reader.Close(); 
            dao.DaoClose(); 

        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void label10_Click(object sender, EventArgs e)
        {

        }

        private void FormLogon_Load(object sender, EventArgs e)
        {
                                                            
        }

        private void buttonexit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textcard_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void textaddress_TextChanged(object sender, EventArgs e)
        {

        }

        private void textphone_TextChanged(object sender, EventArgs e)
        {

        }

        private void textId_TextChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void textpassword_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
